package com.rocket.demo2;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer2 {
    public static void main(String[] args) throws Exception {
        DefaultMQPushConsumer mqPushConsumer = new DefaultMQPushConsumer("consumer2");
        mqPushConsumer.setNamesrvAddr("192.168.126.137:9876");
        mqPushConsumer.subscribe("Tipic2", "*");

        mqPushConsumer.registerMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {

                for (MessageExt msgExt: list) {
                    String msg = new String(msgExt.getBody());
                    System.out.println("收到: "+msg);
                }
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });
        mqPushConsumer.start();
        System.out.println("开始消费");
    }
}
